home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / info / lispref.info-18 < prev    next >
Encoding:
GNU Info File  |  1995-09-01  |  44.3 KB  |  1,063 lines

  1. This is Info file ../../info/lispref.info, produced by Makeinfo-1.63
  2. from the input file lispref.texi.
  3.  
  4.    Edition History:
  5.  
  6.    GNU Emacs Lisp Reference Manual Second Edition (v2.01), May 1993 GNU
  7. Emacs Lisp Reference Manual Further Revised (v2.02), August 1993 Lucid
  8. Emacs Lisp Reference Manual (for 19.10) First Edition, March 1994
  9. XEmacs Lisp Programmer's Manual (for 19.12) Second Edition, April 1995
  10. GNU Emacs Lisp Reference Manual v2.4, June 1995 XEmacs Lisp
  11. Programmer's Manual (for 19.13) Third Edition, July 1995
  12.  
  13.    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995 Free Software
  14. Foundation, Inc.  Copyright (C) 1994, 1995 Sun Microsystems, Inc.
  15. Copyright (C) 1995 Amdahl Corporation.  Copyright (C) 1995 Ben Wing.
  16.  
  17.    Permission is granted to make and distribute verbatim copies of this
  18. manual provided the copyright notice and this permission notice are
  19. preserved on all copies.
  20.  
  21.    Permission is granted to copy and distribute modified versions of
  22. this manual under the conditions for verbatim copying, provided that the
  23. entire resulting derived work is distributed under the terms of a
  24. permission notice identical to this one.
  25.  
  26.    Permission is granted to copy and distribute translations of this
  27. manual into another language, under the above conditions for modified
  28. versions, except that this permission notice may be stated in a
  29. translation approved by the Foundation.
  30.  
  31.    Permission is granted to copy and distribute modified versions of
  32. this manual under the conditions for verbatim copying, provided also
  33. that the section entitled "GNU General Public License" is included
  34. exactly as in the original, and provided that the entire resulting
  35. derived work is distributed under the terms of a permission notice
  36. identical to this one.
  37.  
  38.    Permission is granted to copy and distribute translations of this
  39. manual into another language, under the above conditions for modified
  40. versions, except that the section entitled "GNU General Public License"
  41. may be included in a translation approved by the Free Software
  42. Foundation instead of in the original English.
  43.  
  44. 
  45. File: lispref.info,  Node: Major Mode Conventions,  Next: Example Major Modes,  Up: Major Modes
  46.  
  47. Major Mode Conventions
  48. ----------------------
  49.  
  50.    The code for existing major modes follows various coding conventions,
  51. including conventions for local keymap and syntax table initialization,
  52. global names, and hooks.  Please follow these conventions when you
  53. define a new major mode:
  54.  
  55.    * Define a command whose name ends in `-mode', with no arguments,
  56.      that switches to the new mode in the current buffer.  This command
  57.      should set up the keymap, syntax table, and local variables in an
  58.      existing buffer without changing the buffer's text.
  59.  
  60.    * Write a documentation string for this command that describes the
  61.      special commands available in this mode.  `C-h m'
  62.      (`describe-mode') in your mode will display this string.
  63.  
  64.      The documentation string may include the special documentation
  65.      substrings, `\[COMMAND]', `\{KEYMAP}', and `\<KEYMAP>', that
  66.      enable the documentation to adapt automatically to the user's own
  67.      key bindings.  *Note Keys in Documentation::.
  68.  
  69.    * The major mode command should start by calling
  70.      `kill-all-local-variables'.  This is what gets rid of the local
  71.      variables of the major mode previously in effect.
  72.  
  73.    * The major mode command should set the variable `major-mode' to the
  74.      major mode command symbol.  This is how `describe-mode' discovers
  75.      which documentation to print.
  76.  
  77.    * The major mode command should set the variable `mode-name' to the
  78.      "pretty" name of the mode, as a string.  This appears in the mode
  79.      line.
  80.  
  81.    * Since all global names are in the same name space, all the global
  82.      variables, constants, and functions that are part of the mode
  83.      should have names that start with the major mode name (or with an
  84.      abbreviation of it if the name is long).  *Note Style Tips::.
  85.  
  86.    * The major mode should usually have its own keymap, which is used
  87.      as the local keymap in all buffers in that mode.  The major mode
  88.      function should call `use-local-map' to install this local map.
  89.      *Note Active Keymaps::, for more information.
  90.  
  91.      This keymap should be kept in a global variable named
  92.      `MODENAME-mode-map'.  Normally the library that defines the mode
  93.      sets this variable.
  94.  
  95.    * The mode may have its own syntax table or may share one with other
  96.      related modes.  If it has its own syntax table, it should store
  97.      this in a variable named `MODENAME-mode-syntax-table'.  *Note
  98.      Syntax Tables::.
  99.  
  100.    * The mode may have its own abbrev table or may share one with other
  101.      related modes.  If it has its own abbrev table, it should store
  102.      this in a variable named `MODENAME-mode-abbrev-table'.  *Note
  103.      Abbrev Tables::.
  104.  
  105.    * Use `defvar' to set mode-related variables, so that they are not
  106.      reinitialized if they already have a value.  (Such reinitialization
  107.      could discard customizations made by the user.)
  108.  
  109.    * To make a buffer-local binding for an Emacs customization
  110.      variable, use `make-local-variable' in the major mode command, not
  111.      `make-variable-buffer-local'.  The latter function would make the
  112.      variable local to every buffer in which it is subsequently set,
  113.      which would affect buffers that do not use this mode.  It is
  114.      undesirable for a mode to have such global effects.  *Note
  115.      Buffer-Local Variables::.
  116.  
  117.      It's ok to use `make-variable-buffer-local', if you wish, for a
  118.      variable used only within a single Lisp package.
  119.  
  120.    * Each major mode should have a "mode hook" named
  121.      `MODENAME-mode-hook'.  The major mode command should run that
  122.      hook, with `run-hooks', as the very last thing it does. *Note
  123.      Hooks::.
  124.  
  125.    * The major mode command may also run the hooks of some more basic
  126.      modes.  For example, `indented-text-mode' runs `text-mode-hook' as
  127.      well as `indented-text-mode-hook'.  It may run these other hooks
  128.      immediately before the mode's own hook (that is, after everything
  129.      else), or it may run them earlier.
  130.  
  131.    * If something special should be done if the user switches a buffer
  132.      from this mode to any other major mode, the mode can set a local
  133.      value for `change-major-mode-hook'.
  134.  
  135.    * If this mode is appropriate only for specially-prepared text, then
  136.      the major mode command symbol should have a property named
  137.      `mode-class' with value `special', put on as follows:
  138.  
  139.           (put 'funny-mode 'mode-class 'special)
  140.  
  141.      This tells XEmacs that new buffers created while the current
  142.      buffer has Funny mode should not inherit Funny mode.  Modes such
  143.      as Dired, Rmail, and Buffer List use this feature.
  144.  
  145.    * If you want to make the new mode the default for files with certain
  146.      recognizable names, add an element to `auto-mode-alist' to select
  147.      the mode for those file names.  If you define the mode command to
  148.      autoload, you should add this element in the same file that calls
  149.      `autoload'.  Otherwise, it is sufficient to add the element in the
  150.      file that contains the mode definition.  *Note Auto Major Mode::.
  151.  
  152.    * In the documentation, you should provide a sample `autoload' form
  153.      and an example of how to add to `auto-mode-alist', that users can
  154.      include in their `.emacs' files.
  155.  
  156.    * The top-level forms in the file defining the mode should be
  157.      written so that they may be evaluated more than once without
  158.      adverse consequences.  Even if you never load the file more than
  159.      once, someone else will.
  160.  
  161.  - Variable: change-major-mode-hook
  162.      This normal hook is run by `kill-all-local-variables' before it
  163.      does anything else.  This gives major modes a way to arrange for
  164.      something special to be done if the user switches to a different
  165.      major mode.  For best results, make this variable buffer-local, so
  166.      that it will disappear after doing its job and will not interfere
  167.      with the subsequent major mode.  *Note Hooks::.
  168.  
  169. 
  170. File: lispref.info,  Node: Example Major Modes,  Next: Auto Major Mode,  Prev: Major Mode Conventions,  Up: Major Modes
  171.  
  172. Major Mode Examples
  173. -------------------
  174.  
  175.    Text mode is perhaps the simplest mode besides Fundamental mode.
  176. Here are excerpts from  `text-mode.el' that illustrate many of the
  177. conventions listed above:
  178.  
  179.      ;; Create mode-specific tables.
  180.      (defvar text-mode-syntax-table nil
  181.        "Syntax table used while in text mode.")
  182.  
  183.      (if text-mode-syntax-table
  184.          ()              ; Do not change the table if it is already set up.
  185.        (setq text-mode-syntax-table (make-syntax-table))
  186.        (modify-syntax-entry ?\" ".   " text-mode-syntax-table)
  187.        (modify-syntax-entry ?\\ ".   " text-mode-syntax-table)
  188.        (modify-syntax-entry ?' "w   " text-mode-syntax-table))
  189.  
  190.      (defvar text-mode-abbrev-table nil
  191.        "Abbrev table used while in text mode.")
  192.      (define-abbrev-table 'text-mode-abbrev-table ())
  193.  
  194.      (defvar text-mode-map nil)   ; Create a mode-specific keymap.
  195.      
  196.      (if text-mode-map
  197.          ()              ; Do not change the keymap if it is already set up.
  198.        (setq text-mode-map (make-sparse-keymap))
  199.        (define-key text-mode-map "\t" 'tab-to-tab-stop)
  200.        (define-key text-mode-map "\es" 'center-line)
  201.        (define-key text-mode-map "\eS" 'center-paragraph))
  202.  
  203.    Here is the complete major mode function definition for Text mode:
  204.  
  205.      (defun text-mode ()
  206.        "Major mode for editing text intended for humans to read.
  207.       Special commands: \\{text-mode-map}
  208.  
  209.      Turning on text-mode runs the hook `text-mode-hook'."
  210.        (interactive)
  211.        (kill-all-local-variables)
  212.  
  213.      (use-local-map text-mode-map)     ; This provides the local keymap.
  214.        (setq mode-name "Text")           ; This name goes into the modeline.
  215.        (setq major-mode 'text-mode)      ; This is how `describe-mode'
  216.                                          ;   finds the doc string to print.
  217.        (setq local-abbrev-table text-mode-abbrev-table)
  218.        (set-syntax-table text-mode-syntax-table)
  219.        (run-hooks 'text-mode-hook))      ; Finally, this permits the user to
  220.                                          ;   customize the mode with a hook.
  221.  
  222.    The three Lisp modes (Lisp mode, Emacs Lisp mode, and Lisp
  223. Interaction mode) have more features than Text mode and the code is
  224. correspondingly more complicated.  Here are excerpts from
  225. `lisp-mode.el' that illustrate how these modes are written.
  226.  
  227.      ;; Create mode-specific table variables.
  228.      (defvar lisp-mode-syntax-table nil "")
  229.      (defvar emacs-lisp-mode-syntax-table nil "")
  230.      (defvar lisp-mode-abbrev-table nil "")
  231.  
  232.      (if (not emacs-lisp-mode-syntax-table) ; Do not change the table
  233.                                             ;   if it is already set.
  234.          (let ((i 0))
  235.            (setq emacs-lisp-mode-syntax-table (make-syntax-table))
  236.  
  237.      ;; Set syntax of chars up to 0 to class of chars that are
  238.            ;;   part of symbol names but not words.
  239.            ;;   (The number 0 is `48' in the ASCII character set.)
  240.            (while (< i ?0)
  241.              (modify-syntax-entry i "_   " emacs-lisp-mode-syntax-table)
  242.              (setq i (1+ i)))
  243.            ...
  244.  
  245.      ;; Set the syntax for other characters.
  246.            (modify-syntax-entry ?  "    " emacs-lisp-mode-syntax-table)
  247.            (modify-syntax-entry ?\t "    " emacs-lisp-mode-syntax-table)
  248.            ...
  249.  
  250.      (modify-syntax-entry ?\( "()  " emacs-lisp-mode-syntax-table)
  251.            (modify-syntax-entry ?\) ")(  " emacs-lisp-mode-syntax-table)
  252.            ...))
  253.      ;; Create an abbrev table for lisp-mode.
  254.      (define-abbrev-table 'lisp-mode-abbrev-table ())
  255.  
  256.    Much code is shared among the three Lisp modes.  The following
  257. function sets various variables; it is called by each of the major Lisp
  258. mode functions:
  259.  
  260.      (defun lisp-mode-variables (lisp-syntax)
  261.        ;; The `lisp-syntax' argument is `nil' in Emacs Lisp mode,
  262.        ;;   and `t' in the other two Lisp modes.
  263.        (cond (lisp-syntax
  264.               (if (not lisp-mode-syntax-table)
  265.                   ;; The Emacs Lisp mode syntax table always exists, but
  266.                   ;;   the Lisp Mode syntax table is created the first time a
  267.                   ;;   mode that needs it is called.  This is to save space.
  268.  
  269.      (progn (setq lisp-mode-syntax-table
  270.                             (copy-syntax-table emacs-lisp-mode-syntax-table))
  271.                          ;; Change some entries for Lisp mode.
  272.                          (modify-syntax-entry ?\| "\"   "
  273.                                               lisp-mode-syntax-table)
  274.                          (modify-syntax-entry ?\[ "_   "
  275.                                               lisp-mode-syntax-table)
  276.                          (modify-syntax-entry ?\] "_   "
  277.                                               lisp-mode-syntax-table)))
  278.  
  279.      (set-syntax-table lisp-mode-syntax-table)))
  280.        (setq local-abbrev-table lisp-mode-abbrev-table)
  281.        ...)
  282.  
  283.    Functions such as `forward-paragraph' use the value of the
  284. `paragraph-start' variable.  Since Lisp code is different from ordinary
  285. text, the `paragraph-start' variable needs to be set specially to
  286. handle Lisp.  Also, comments are indented in a special fashion in Lisp
  287. and the Lisp modes need their own mode-specific
  288. `comment-indent-function'.  The code to set these variables is the rest
  289. of `lisp-mode-variables'.
  290.  
  291.      (make-local-variable 'paragraph-start)
  292.        ;; Having `^' is not clean, but `page-delimiter'
  293.        ;; has them too, and removing those is a pain.
  294.        (setq paragraph-start (concat "^$\\|" page-delimiter))
  295.        ...
  296.  
  297.      (make-local-variable 'comment-indent-function)
  298.        (setq comment-indent-function 'lisp-comment-indent))
  299.  
  300.    Each of the different Lisp modes has a slightly different keymap.
  301. For example, Lisp mode binds `C-c C-l' to `run-lisp', but the other
  302. Lisp modes do not.  However, all Lisp modes have some commands in
  303. common.  The following function adds these common commands to a given
  304. keymap.
  305.  
  306.      (defun lisp-mode-commands (map)
  307.        (define-key map "\e\C-q" 'indent-sexp)
  308.        (define-key map "\177" 'backward-delete-char-untabify)
  309.        (define-key map "\t" 'lisp-indent-line))
  310.  
  311.    Here is an example of using `lisp-mode-commands' to initialize a
  312. keymap, as part of the code for Emacs Lisp mode.  First we declare a
  313. variable with `defvar' to hold the mode-specific keymap.  When this
  314. `defvar' executes, it sets the variable to `nil' if it was void.  Then
  315. we set up the keymap if the variable is `nil'.
  316.  
  317.    This code avoids changing the keymap or the variable if it is already
  318. set up.  This lets the user customize the keymap.
  319.  
  320.      (defvar emacs-lisp-mode-map () "")
  321.      (if emacs-lisp-mode-map
  322.          ()
  323.        (setq emacs-lisp-mode-map (make-sparse-keymap))
  324.        (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
  325.        (lisp-mode-commands emacs-lisp-mode-map))
  326.  
  327.    Finally, here is the complete major mode function definition for
  328. Emacs Lisp mode.
  329.  
  330.      (defun emacs-lisp-mode ()
  331.        "Major mode for editing Lisp code to run in XEmacs.
  332.      Commands:
  333.      Delete converts tabs to spaces as it moves back.
  334.      Blank lines separate paragraphs.  Semicolons start comments.
  335.      \\{emacs-lisp-mode-map}
  336.  
  337.      Entry to this mode runs the hook `emacs-lisp-mode-hook'."
  338.        (interactive)
  339.        (kill-all-local-variables)
  340.        (use-local-map emacs-lisp-mode-map)    ; This provides the local keymap.
  341.        (set-syntax-table emacs-lisp-mode-syntax-table)
  342.  
  343.      (setq major-mode 'emacs-lisp-mode)     ; This is how `describe-mode'
  344.                                               ;   finds out what to describe.
  345.        (setq mode-name "Emacs-Lisp")          ; This goes into the modeline.
  346.        (lisp-mode-variables nil)              ; This defines various variables.
  347.        (run-hooks 'emacs-lisp-mode-hook))     ; This permits the user to use a
  348.                                               ;   hook to customize the mode.
  349.  
  350. 
  351. File: lispref.info,  Node: Auto Major Mode,  Next: Mode Help,  Prev: Example Major Modes,  Up: Major Modes
  352.  
  353. How XEmacs Chooses a Major Mode
  354. -------------------------------
  355.  
  356.    Based on information in the file name or in the file itself, XEmacs
  357. automatically selects a major mode for the new buffer when a file is
  358. visited.
  359.  
  360.  - Command: fundamental-mode
  361.      Fundamental mode is a major mode that is not specialized for
  362.      anything in particular.  Other major modes are defined in effect
  363.      by comparison with this one--their definitions say what to change,
  364.      starting from Fundamental mode.  The `fundamental-mode' function
  365.      does *not* run any hooks; you're not supposed to customize it.
  366.      (If you want Emacs to behave differently in Fundamental mode,
  367.      change the *global* state of Emacs.)
  368.  
  369.  - Command: normal-mode &optional FIND-FILE
  370.      This function establishes the proper major mode and local variable
  371.      bindings for the current buffer.  First it calls `set-auto-mode',
  372.      then it runs `hack-local-variables' to parse, and bind or evaluate
  373.      as appropriate, any local variables.
  374.  
  375.      If the FIND-FILE argument to `normal-mode' is non-`nil',
  376.      `normal-mode' assumes that the `find-file' function is calling it.
  377.      In this case, it may process a local variables list at the end of
  378.      the file and in the `-*-' line.  The variable
  379.      `enable-local-variables' controls whether to do so.
  380.  
  381.      If you run `normal-mode' interactively, the argument FIND-FILE is
  382.      normally `nil'.  In this case, `normal-mode' unconditionally
  383.      processes any local variables list.  *Note Local Variables in
  384.      Files: (emacs)File variables, for the syntax of the local
  385.      variables section of a file.
  386.  
  387.      `normal-mode' uses `condition-case' around the call to the major
  388.      mode function, so errors are caught and reported as a `File mode
  389.      specification error',  followed by the original error message.
  390.  
  391.  - User Option: enable-local-variables
  392.      This variable controls processing of local variables lists in files
  393.      being visited.  A value of `t' means process the local variables
  394.      lists unconditionally; `nil' means ignore them; anything else means
  395.      ask the user what to do for each file.  The default value is `t'.
  396.  
  397.  - Variable: ignored-local-variables
  398.      This variable holds a list of variables that should not be set by
  399.      a local variables list.  Any value specified for one of these
  400.      variables is ignored.
  401.  
  402.    In addition to this list, any variable whose name has a non-`nil'
  403. `risky-local-variable' property is also ignored.
  404.  
  405.  - User Option: enable-local-eval
  406.      This variable controls processing of `Eval:' in local variables
  407.      lists in files being visited.  A value of `t' means process them
  408.      unconditionally; `nil' means ignore them; anything else means ask
  409.      the user what to do for each file.  The default value is `maybe'.
  410.  
  411.  - Function: set-auto-mode
  412.      This function selects the major mode that is appropriate for the
  413.      current buffer.  It may base its decision on the value of the `-*-'
  414.      line, on the visited file name (using `auto-mode-alist'), or on the
  415.      value of a local variable.  However, this function does not look
  416.      for the `mode:' local variable near the end of a file; the
  417.      `hack-local-variables' function does that.  *Note How Major Modes
  418.      are Chosen: (emacs)Choosing Modes.
  419.  
  420.  - User Option: default-major-mode
  421.      This variable holds the default major mode for new buffers.  The
  422.      standard value is `fundamental-mode'.
  423.  
  424.      If the value of `default-major-mode' is `nil', XEmacs uses the
  425.      (previously) current buffer's major mode for the major mode of a
  426.      new buffer.  However, if the major mode symbol has a `mode-class'
  427.      property with value `special', then it is not used for new buffers;
  428.      Fundamental mode is used instead.  The modes that have this
  429.      property are those such as Dired and Rmail that are useful only
  430.      with text that has been specially prepared.
  431.  
  432.  - Function: set-buffer-major-mode BUFFER
  433.      This function sets the major mode of BUFFER to the value of
  434.      `default-major-mode'.  If that variable is `nil', it uses the
  435.      current buffer's major mode (if that is suitable).
  436.  
  437.      The low-level primitives for creating buffers do not use this
  438.      function, but medium-level commands such as `switch-to-buffer' and
  439.      `find-file-noselect' use it whenever they create buffers.
  440.  
  441.  - Variable: initial-major-mode
  442.      The value of this variable determines the major mode of the initial
  443.      `*scratch*' buffer.  The value should be a symbol that is a major
  444.      mode command name.  The default value is `lisp-interaction-mode'.
  445.  
  446.  - Variable: auto-mode-alist
  447.      This variable contains an association list of file name patterns
  448.      (regular expressions; *note Regular Expressions::.) and
  449.      corresponding major mode functions.  Usually, the file name
  450.      patterns test for suffixes, such as `.el' and `.c', but this need
  451.      not be the case.  An ordinary element of the alist looks like
  452.      `(REGEXP .  MODE-FUNCTION)'.
  453.  
  454.      For example,
  455.  
  456.           (("^/tmp/fol/" . text-mode)
  457.            ("\\.texinfo\\'" . texinfo-mode)
  458.            ("\\.texi\\'" . texinfo-mode)
  459.  
  460.           ("\\.el\\'" . emacs-lisp-mode)
  461.            ("\\.c\\'" . c-mode)
  462.            ("\\.h\\'" . c-mode)
  463.            ...)
  464.  
  465.      When you visit a file whose expanded file name (*note File Name
  466.      Expansion::.) matches a REGEXP, `set-auto-mode' calls the
  467.      corresponding MODE-FUNCTION.  This feature enables XEmacs to select
  468.      the proper major mode for most files.
  469.  
  470.      If an element of `auto-mode-alist' has the form `(REGEXP FUNCTION
  471.      t)', then after calling FUNCTION, XEmacs searches
  472.      `auto-mode-alist' again for a match against the portion of the file
  473.      name that did not match before.
  474.  
  475.      This match-again feature is useful for uncompression packages: an
  476.      entry of the form `("\\.gz\\'" . FUNCTION)' can uncompress the file
  477.      and then put the uncompressed file in the proper mode according to
  478.      the name sans `.gz'.
  479.  
  480.      Here is an example of how to prepend several pattern pairs to
  481.      `auto-mode-alist'.  (You might use this sort of expression in your
  482.      `.emacs' file.)
  483.  
  484.           (setq auto-mode-alist
  485.             (append
  486.              ;; File name starts with a dot.
  487.              '(("/\\.[^/]*\\'" . fundamental-mode)
  488.                ;; File name has no dot.
  489.                ("[^\\./]*\\'" . fundamental-mode)
  490.                ;; File name ends in `.C'.
  491.                ("\\.C\\'" . c++-mode))
  492.              auto-mode-alist))
  493.  
  494.  - Variable: interpreter-mode-alist
  495.      This variable specifes major modes to use for scripts that specify
  496.      a command interpreter in an `!#' line.  Its value is a list of
  497.      elements of the form `(INTERPRETER . MODE)'; for example, `("perl"
  498.      . perl-mode)' is one element present by default.  The element says
  499.      to use mode MODE if the file specifies INTERPRETER.
  500.  
  501.      This variable is applicable only when the `auto-mode-alist' does
  502.      not indicate which major mode to use.
  503.  
  504.  - Function: hack-local-variables &optional FORCE
  505.      This function parses, and binds or evaluates as appropriate, any
  506.      local variables for the current buffer.
  507.  
  508.      The handling of `enable-local-variables' documented for
  509.      `normal-mode' actually takes place here.  The argument FORCE
  510.      usually comes from the argument FIND-FILE given to `normal-mode'.
  511.  
  512. 
  513. File: lispref.info,  Node: Mode Help,  Next: Derived Modes,  Prev: Auto Major Mode,  Up: Major Modes
  514.  
  515. Getting Help about a Major Mode
  516. -------------------------------
  517.  
  518.    The `describe-mode' function is used to provide information about
  519. major modes.  It is normally called with `C-h m'.  The `describe-mode'
  520. function uses the value of `major-mode', which is why every major mode
  521. function needs to set the `major-mode' variable.
  522.  
  523.  - Command: describe-mode
  524.      This function displays the documentation of the current major mode.
  525.  
  526.      The `describe-mode' function calls the `documentation' function
  527.      using the value of `major-mode' as an argument.  Thus, it displays
  528.      the documentation string of the major mode function.  (*Note
  529.      Accessing Documentation::.)
  530.  
  531.  - Variable: major-mode
  532.      This variable holds the symbol for the current buffer's major mode.
  533.      This symbol should have a function definition that is the command
  534.      to switch to that major mode.  The `describe-mode' function uses
  535.      the documentation string of the function as the documentation of
  536.      the major mode.
  537.  
  538. 
  539. File: lispref.info,  Node: Derived Modes,  Prev: Mode Help,  Up: Major Modes
  540.  
  541. Defining Derived Modes
  542. ----------------------
  543.  
  544.    It's often useful to define a new major mode in terms of an existing
  545. one.  An easy way to do this is to use `define-derived-mode'.
  546.  
  547.  - Macro: define-derived-mode VARIANT PARENT NAME DOCSTRING BODY...
  548.      This construct defines VARIANT as a major mode command, using NAME
  549.      as the string form of the mode name.
  550.  
  551.      The new command VARIANT is defined to call the function PARENT,
  552.      then override certain aspects of that parent mode:
  553.  
  554.         * The new mode has its own keymap, named `VARIANT-map'.
  555.           `define-derived-mode' initializes this map to inherit from
  556.           `PARENT-map', if it is not already set.
  557.  
  558.         * The new mode has its own syntax table, kept in the variable
  559.           `VARIANT-syntax-table'.  `define-derived-mode' initializes
  560.           this variable by copying `PARENT-syntax-table', if it is not
  561.           already set.
  562.  
  563.         * The new mode has its own abbrev table, kept in the variable
  564.           `VARIANT-abbrev-table'.  `define-derived-mode' initializes
  565.           this variable by copying `PARENT-abbrev-table', if it is not
  566.           already set.
  567.  
  568.         * The new mode has its own mode hook, `VARIANT-hook', which it
  569.           runs in standard fashion as the very last thing that it does.
  570.           (The new mode also runs the mode hook of PARENT as part of
  571.           calling PARENT.)
  572.  
  573.      In addition, you can specify how to override other aspects of
  574.      PARENT with BODY.  The command VARIANT evaluates the forms in BODY
  575.      after setting up all its usual overrides, just before running
  576.      `VARIANT-hook'.
  577.  
  578.      The argument DOCSTRING specifies the documentation string for the
  579.      new mode.  If you omit DOCSTRING, `define-derived-mode' generates
  580.      a documentation string.
  581.  
  582.      Here is a hypothetical example:
  583.  
  584.           (define-derived-mode hypertext-mode
  585.             text-mode "Hypertext"
  586.             "Major mode for hypertext.
  587.           \\{hypertext-mode-map}"
  588.             (setq case-fold-search nil))
  589.           
  590.           (define-key hypertext-mode-map
  591.             [down-mouse-3] 'do-hyper-link)
  592.  
  593. 
  594. File: lispref.info,  Node: Minor Modes,  Next: Modeline Format,  Prev: Major Modes,  Up: Modes
  595.  
  596. Minor Modes
  597. ===========
  598.  
  599.    A "minor mode" provides features that users may enable or disable
  600. independently of the choice of major mode.  Minor modes can be enabled
  601. individually or in combination.  Minor modes would be better named
  602. "Generally available, optional feature modes" except that such a name is
  603. unwieldy.
  604.  
  605.    A minor mode is not usually a modification of single major mode.  For
  606. example, Auto Fill mode may be used in any major mode that permits text
  607. insertion.  To be general, a minor mode must be effectively independent
  608. of the things major modes do.
  609.  
  610.    A minor mode is often much more difficult to implement than a major
  611. mode.  One reason is that you should be able to activate and deactivate
  612. minor modes in any order.  A minor mode should be able to have its
  613. desired effect regardless of the major mode and regardless of the other
  614. minor modes in effect.
  615.  
  616.    Often the biggest problem in implementing a minor mode is finding a
  617. way to insert the necessary hook into the rest of XEmacs.  Minor mode
  618. keymaps make this easier than it used to be.
  619.  
  620. * Menu:
  621.  
  622. * Minor Mode Conventions::      Tips for writing a minor mode.
  623. * Keymaps and Minor Modes::     How a minor mode can have its own keymap.
  624.  
  625. 
  626. File: lispref.info,  Node: Minor Mode Conventions,  Next: Keymaps and Minor Modes,  Up: Minor Modes
  627.  
  628. Conventions for Writing Minor Modes
  629. -----------------------------------
  630.  
  631.    There are conventions for writing minor modes just as there are for
  632. major modes.  Several of the major mode conventions apply to minor
  633. modes as well: those regarding the name of the mode initialization
  634. function, the names of global symbols, and the use of keymaps and other
  635. tables.
  636.  
  637.    In addition, there are several conventions that are specific to
  638. minor modes.
  639.  
  640.    * Make a variable whose name ends in `-mode' to represent the minor
  641.      mode.  Its value should enable or disable the mode (`nil' to
  642.      disable; anything else to enable.)  We call this the "mode
  643.      variable".
  644.  
  645.      This variable is used in conjunction with the `minor-mode-alist' to
  646.      display the minor mode name in the modeline.  It can also enable
  647.      or disable a minor mode keymap.  Individual commands or hooks can
  648.      also check the variable's value.
  649.  
  650.      If you want the minor mode to be enabled separately in each buffer,
  651.      make the variable buffer-local.
  652.  
  653.    * Define a command whose name is the same as the mode variable.  Its
  654.      job is to enable and disable the mode by setting the variable.
  655.  
  656.      The command should accept one optional argument.  If the argument
  657.      is `nil', it should toggle the mode (turn it on if it is off, and
  658.      off if it is on).  Otherwise, it should turn the mode on if the
  659.      argument is a positive integer, a symbol other than `nil' or `-',
  660.      or a list whose CAR is such an integer or symbol; it should turn
  661.      the mode off otherwise.
  662.  
  663.      Here is an example taken from the definition of
  664.      `transient-mark-mode'.  It shows the use of `transient-mark-mode'
  665.      as a variable that enables or disables the mode's behavior, and
  666.      also shows the proper way to toggle, enable or disable the minor
  667.      mode based on the raw prefix argument value.
  668.  
  669.           (setq transient-mark-mode
  670.                 (if (null arg) (not transient-mark-mode)
  671.                   (> (prefix-numeric-value arg) 0)))
  672.  
  673.    * Add an element to `minor-mode-alist' for each minor mode (*note
  674.      Modeline Variables::.).  This element should be a list of the
  675.      following form:
  676.  
  677.           (MODE-VARIABLE STRING)
  678.  
  679.      Here MODE-VARIABLE is the variable that controls enabling of the
  680.      minor mode, and STRING is a short string, starting with a space,
  681.      to represent the mode in the modeline.  These strings must be
  682.      short so that there is room for several of them at once.
  683.  
  684.      When you add an element to `minor-mode-alist', use `assq' to check
  685.      for an existing element, to avoid duplication.  For example:
  686.  
  687.           (or (assq 'leif-mode minor-mode-alist)
  688.               (setq minor-mode-alist
  689.                     (cons '(leif-mode " Leif") minor-mode-alist)))
  690.  
  691. 
  692. File: lispref.info,  Node: Keymaps and Minor Modes,  Prev: Minor Mode Conventions,  Up: Minor Modes
  693.  
  694. Keymaps and Minor Modes
  695. -----------------------
  696.  
  697.    Each minor mode can have its own keymap, which is active when the
  698. mode is enabled.  To set up a keymap for a minor mode, add an element
  699. to the alist `minor-mode-map-alist'.  *Note Active Keymaps::.
  700.  
  701.    One use of minor mode keymaps is to modify the behavior of certain
  702. self-inserting characters so that they do something else as well as
  703. self-insert.  In general, this is the only way to do that, since the
  704. facilities for customizing `self-insert-command' are limited to special
  705. cases (designed for abbrevs and Auto Fill mode).  (Do not try
  706. substituting your own definition of `self-insert-command' for the
  707. standard one.  The editor command loop handles this function specially.)
  708.  
  709. 
  710. File: lispref.info,  Node: Modeline Format,  Next: Hooks,  Prev: Minor Modes,  Up: Modes
  711.  
  712. Modeline Format
  713. ===============
  714.  
  715.    Each Emacs window (aside from minibuffer windows) includes a
  716. modeline, which displays status information about the buffer displayed
  717. in the window.  The modeline contains information about the buffer,
  718. such as its name, associated file, depth of recursive editing, and the
  719. major and minor modes.
  720.  
  721.    This section describes how the contents of the modeline are
  722. controlled.  It is in the chapter on modes because much of the
  723. information displayed in the modeline relates to the enabled major and
  724. minor modes.
  725.  
  726.    `modeline-format' is a buffer-local variable that holds a template
  727. used to display the modeline of the current buffer.  All windows for
  728. the same buffer use the same `modeline-format' and their modelines
  729. appear the same (except for scrolling percentages and line numbers).
  730.  
  731.    The modeline of a window is normally updated whenever a different
  732. buffer is shown in the window, or when the buffer's modified-status
  733. changes from `nil' to `t' or vice-versa.  If you modify any of the
  734. variables referenced by `modeline-format' (*note Modeline
  735. Variables::.), you may want to force an update of the modeline so as to
  736. display the new information.
  737.  
  738.  - Function: redraw-modeline &optional ALL
  739.      Force redisplay of the current buffer's modeline.  If ALL is
  740.      non-`nil', then force redisplay of all modelines.
  741.  
  742.    The modeline is usually displayed in inverse video.  This is
  743. controlled using the `modeline' face.  *Note Faces::.
  744.  
  745. * Menu:
  746.  
  747. * Modeline Data::         The data structure that controls the modeline.
  748. * Modeline Variables::    Variables used in that data structure.
  749. * %-Constructs::          Putting information into a modeline.
  750.  
  751. 
  752. File: lispref.info,  Node: Modeline Data,  Next: Modeline Variables,  Up: Modeline Format
  753.  
  754. The Data Structure of the Modeline
  755. ----------------------------------
  756.  
  757.    The modeline contents are controlled by a data structure of lists,
  758. strings, symbols, and numbers kept in the buffer-local variable
  759. `mode-line-format'.  The data structure is called a "modeline
  760. construct", and it is built in recursive fashion out of simpler modeline
  761. constructs.  The same data structure is used for constructing frame
  762. titles (*note Frame Titles::.).
  763.  
  764.  - Variable: modeline-format
  765.      The value of this variable is a modeline construct with overall
  766.      responsibility for the modeline format.  The value of this variable
  767.      controls which other variables are used to form the modeline text,
  768.      and where they appear.
  769.  
  770.    A modeline construct may be as simple as a fixed string of text, but
  771. it usually specifies how to use other variables to construct the text.
  772. Many of these variables are themselves defined to have modeline
  773. constructs as their values.
  774.  
  775.    The default value of `modeline-format' incorporates the values of
  776. variables such as `mode-name' and `minor-mode-alist'.  Because of this,
  777. very few modes need to alter `modeline-format'.  For most purposes, it
  778. is sufficient to alter the variables referenced by `modeline-format'.
  779.  
  780.    A modeline construct may be a list, a symbol, or a string.  If the
  781. value is a list, each element may be a list, a symbol, or a string.
  782.  
  783. `STRING'
  784.      A string as a modeline construct is displayed verbatim in the mode
  785.      line except for "`%'-constructs".  Decimal digits after the `%'
  786.      specify the field width for space filling on the right (i.e., the
  787.      data is left justified).  *Note %-Constructs::.
  788.  
  789. `SYMBOL'
  790.      A symbol as a modeline construct stands for its value.  The value
  791.      of SYMBOL is used as a modeline construct, in place of SYMBOL.
  792.      However, the symbols `t' and `nil' are ignored; so is any symbol
  793.      whose value is void.
  794.  
  795.      There is one exception: if the value of SYMBOL is a string, it is
  796.      displayed verbatim: the `%'-constructs are not recognized.
  797.  
  798. `(STRING REST...) or (LIST REST...)'
  799.      A list whose first element is a string or list means to process
  800.      all the elements recursively and concatenate the results.  This is
  801.      the most common form of mode line construct.
  802.  
  803. `(SYMBOL THEN ELSE)'
  804.      A list whose first element is a symbol is a conditional.  Its
  805.      meaning depends on the value of SYMBOL.  If the value is non-`nil',
  806.      the second element, THEN, is processed recursively as a modeline
  807.      element.  But if the value of SYMBOL is `nil', the third element,
  808.      ELSE, is processed recursively.  You may omit ELSE; then the mode
  809.      line element displays nothing if the value of SYMBOL is `nil'.
  810.  
  811. `(WIDTH REST...)'
  812.      A list whose first element is an integer specifies truncation or
  813.      padding of the results of REST.  The remaining elements REST are
  814.      processed recursively as modeline constructs and concatenated
  815.      together.  Then the result is space filled (if WIDTH is positive)
  816.      or truncated (to -WIDTH columns, if WIDTH is negative) on the
  817.      right.
  818.  
  819.      For example, the usual way to show what percentage of a buffer is
  820.      above the top of the window is to use a list like this: `(-3
  821.      "%p")'.
  822.  
  823.    If you do alter `modeline-format' itself, the new value should use
  824. the same variables that appear in the default value (*note Modeline
  825. Variables::.), rather than duplicating their contents or displaying the
  826. information in another fashion.  This way, customizations made by the
  827. user or by Lisp programs (such as `display-time' and major modes) via
  828. changes to those variables remain effective.
  829.  
  830.    Here is an example of a `modeline-format' that might be useful for
  831. `shell-mode', since it contains the hostname and default directory.
  832.  
  833.      (setq modeline-format
  834.        (list ""
  835.         'modeline-modified
  836.         "%b--"
  837.         (getenv "HOST")      ; One element is not constant.
  838.         ":"
  839.         'default-directory
  840.         "   "
  841.         'global-mode-string
  842.         "   %[("
  843.         'mode-name
  844.         'modeline-process
  845.         'minor-mode-alist
  846.         "%n"
  847.         ")%]----"
  848.         '(line-number-mode "L%l--")
  849.         '(-3 . "%p")
  850.         "-%-"))
  851.  
  852. 
  853. File: lispref.info,  Node: Modeline Variables,  Next: %-Constructs,  Prev: Modeline Data,  Up: Modeline Format
  854.  
  855. Variables Used in the Modeline
  856. ------------------------------
  857.  
  858.    This section describes variables incorporated by the standard value
  859. of `modeline-format' into the text of the mode line.  There is nothing
  860. inherently special about these variables; any other variables could
  861. have the same effects on the modeline if `modeline-format' were changed
  862. to use them.
  863.  
  864.  - Variable: modeline-modified
  865.      This variable holds the value of the modeline construct that
  866.      displays whether the current buffer is modified.
  867.  
  868.      The default value of `modeline-modified' is `("--%1*%1+-")'.  This
  869.      means that the modeline displays `--**-' if the buffer is
  870.      modified, `-----' if the buffer is not modified, `--%%-' if the
  871.      buffer is read only, and `--%*--' if the buffer is read only and
  872.      modified.
  873.  
  874.      Changing this variable does not force an update of the modeline.
  875.  
  876.  - Variable: modeline-buffer-identification
  877.      This variable identifies the buffer being displayed in the window.
  878.      Its default value is `("%F: %17b")', which means that it usually
  879.      displays `Emacs:' followed by seventeen characters of the buffer
  880.      name.  (In a terminal frame, it displays the frame name instead of
  881.      `Emacs'; this has the effect of showing the frame number.)  You may
  882.      want to change this in modes such as Rmail that do not behave like
  883.      a "normal" XEmacs.
  884.  
  885.  - Variable: global-mode-string
  886.      This variable holds a modeline spec that appears in the mode line
  887.      by default, just after the buffer name.  The command `display-time'
  888.      sets `global-mode-string' to refer to the variable
  889.      `display-time-string', which holds a string containing the time and
  890.      load information.
  891.  
  892.      The `%M' construct substitutes the value of `global-mode-string',
  893.      but this is obsolete, since the variable is included directly in
  894.      the modeline.
  895.  
  896.  - Variable: mode-name
  897.      This buffer-local variable holds the "pretty" name of the current
  898.      buffer's major mode.  Each major mode should set this variable so
  899.      that the mode name will appear in the modeline.
  900.  
  901.  - Variable: minor-mode-alist
  902.      This variable holds an association list whose elements specify how
  903.      the modeline should indicate that a minor mode is active.  Each
  904.      element of the `minor-mode-alist' should be a two-element list:
  905.  
  906.           (MINOR-MODE-VARIABLE MODELINE-STRING)
  907.  
  908.      More generally, MODELINE-STRING can be any mode line spec.  It
  909.      appears in the mode line when the value of MINOR-MODE-VARIABLE is
  910.      non-`nil', and not otherwise.  These strings should begin with
  911.      spaces so that they don't run together.  Conventionally, the
  912.      MINOR-MODE-VARIABLE for a specific mode is set to a non-`nil'
  913.      value when that minor mode is activated.
  914.  
  915.      The default value of `minor-mode-alist' is:
  916.  
  917.           minor-mode-alist
  918.           => ((vc-mode vc-mode)
  919.               (abbrev-mode " Abbrev")
  920.               (overwrite-mode overwrite-mode)
  921.               (auto-fill-function " Fill")
  922.               (defining-kbd-macro " Def")
  923.               (isearch-mode isearch-mode))
  924.  
  925.      `minor-mode-alist' is not buffer-local.  The variables mentioned
  926.      in the alist should be buffer-local if the minor mode can be
  927.      enabled separately in each buffer.
  928.  
  929.  - Variable: modeline-process
  930.      This buffer-local variable contains the modeline information on
  931.      process status in modes used for communicating with subprocesses.
  932.      It is displayed immediately following the major mode name, with no
  933.      intervening space.  For example, its value in the `*shell*' buffer
  934.      is `(": %s")', which allows the shell to display its status along
  935.      with the major mode as: `(Shell: run)'.  Normally this variable is
  936.      `nil'.
  937.  
  938.  - Variable: default-modeline-format
  939.      This variable holds the default `modeline-format' for buffers that
  940.      do not override it.  This is the same as `(default-value
  941.      'modeline-format)'.
  942.  
  943.      The default value of `default-modeline-format' is:
  944.  
  945.           (""
  946.            modeline-modified
  947.            modeline-buffer-identification
  948.            "   "
  949.            global-mode-string
  950.            "   %[("
  951.            mode-name
  952.            modeline-process
  953.            minor-mode-alist
  954.            "%n"
  955.            ")%]----"
  956.            (line-number-mode "L%l--")
  957.            (-3 . "%p")
  958.            "-%-")
  959.  
  960.  - Variable: vc-mode
  961.      The variable `vc-mode', local in each buffer, records whether the
  962.      buffer's visited file is maintained with version control, and, if
  963.      so, which kind.  Its value is `nil' for no version control, or a
  964.      string that appears in the mode line.
  965.  
  966. 
  967. File: lispref.info,  Node: %-Constructs,  Prev: Modeline Variables,  Up: Modeline Format
  968.  
  969. `%'-Constructs in the ModeLine
  970. ------------------------------
  971.  
  972.    The following table lists the recognized `%'-constructs and what
  973. they mean.  In any construct except `%%', you can add a decimal integer
  974. after the `%' to specify how many characters to display.
  975.  
  976. `%b'
  977.      The current buffer name, obtained with the `buffer-name' function.
  978.      *Note Buffer Names::.
  979.  
  980. `%f'
  981.      The visited file name, obtained with the `buffer-file-name'
  982.      function.  *Note Buffer File Name::.
  983.  
  984. `%F'
  985.      The name of the selected frame.
  986.  
  987. `%c'
  988.      The current column number of point.
  989.  
  990. `%l'
  991.      The current line number of point.
  992.  
  993. `%*'
  994.      `%' if the buffer is read only (see `buffer-read-only');
  995.      `*' if the buffer is modified (see `buffer-modified-p');
  996.      `-' otherwise.  *Note Buffer Modification::.
  997.  
  998. `%+'
  999.      `*' if the buffer is modified (see `buffer-modified-p');
  1000.      `%' if the buffer is read only (see `buffer-read-only');
  1001.      `-' otherwise.  This differs from `%*' only for a modified
  1002.      read-only buffer.  *Note Buffer Modification::.
  1003.  
  1004. `%&'
  1005.      `*' if the buffer is modified, and `-' otherwise.
  1006.  
  1007. `%s'
  1008.      The status of the subprocess belonging to the current buffer,
  1009.      obtained with `process-status'.  *Note Process Information::.
  1010.  
  1011. `%l'
  1012.      the current line number.
  1013.  
  1014. `%S'
  1015.      the name of the selected frame; this is only meaningful under the
  1016.      X Window System.  *Note Frame Name::.
  1017.  
  1018. `%t'
  1019.      Whether the visited file is a text file or a binary file.  (This
  1020.      is a meaningful distinction only on certain operating systems.)
  1021.  
  1022. `%p'
  1023.      The percentage of the buffer text above the *top* of window, or
  1024.      `Top', `Bottom' or `All'.
  1025.  
  1026. `%P'
  1027.      The percentage of the buffer text that is above the *bottom* of
  1028.      the window (which includes the text visible in the window, as well
  1029.      as the text above the top), plus `Top' if the top of the buffer is
  1030.      visible on screen; or `Bottom' or `All'.
  1031.  
  1032. `%n'
  1033.      `Narrow' when narrowing is in effect; nothing otherwise (see
  1034.      `narrow-to-region' in *Note Narrowing::).
  1035.  
  1036. `%['
  1037.      An indication of the depth of recursive editing levels (not
  1038.      counting minibuffer levels): one `[' for each editing level.
  1039.      *Note Recursive Editing::.
  1040.  
  1041. `%]'
  1042.      One `]' for each recursive editing level (not counting minibuffer
  1043.      levels).
  1044.  
  1045. `%%'
  1046.      The character `%'--this is how to include a literal `%' in a
  1047.      string in which `%'-constructs are allowed.
  1048.  
  1049. `%-'
  1050.      Dashes sufficient to fill the remainder of the modeline.
  1051.  
  1052.    The following two `%'-constructs are still supported, but they are
  1053. obsolete, since you can get the same results with the variables
  1054. `mode-name' and `global-mode-string'.
  1055.  
  1056. `%m'
  1057.      The value of `mode-name'.
  1058.  
  1059. `%M'
  1060.      The value of `global-mode-string'.  Currently, only `display-time'
  1061.      modifies the value of `global-mode-string'.
  1062.  
  1063.